﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Teleplex.Data;
using Teleplex.DataModels;
using Teleplex.Data.DataAdapters.Interfaces;
using Teleplex.Data.DataAdapters.Adapters;

namespace Teleplex.Web.Controllers
{
    public class TVShowController : Controller
    {
        public ITVShowAdapter _adapter;

        public TVShowController()
        {
            _adapter = new TVShowAdapter();
        }
        public TVShowController(TVShowAdapter adapter)
        {
            _adapter = adapter;
        }

        // GET: TVShow
        //TV Show main Index view
        public ActionResult Index()
        {
            List<TVShow> tvShows = _adapter.GetAllShows();

            // TODO: add paging

            return View(tvShows);
        }

        //TV Show Details view
        public ActionResult Detail(int id = 0)
        {
            if (id <= 0)
            {
                return RedirectToAction("Index");
            }

            TVShow tvShow = _adapter.GetShow(id);

            return View(tvShow);
        }

        //TV Show Add view
        [HttpGet]
        public ActionResult AddShow()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddShow(string title, string synopsis, string imageURL)
        {

            TVShow show = new TVShow(title, synopsis, imageURL);
            show = _adapter.CreateShow(show);

            return RedirectToAction("Detail", new { id = show.Id });
        }

        //TV Show Edit view
        public ActionResult EditShow(int id)
        {
            TVShow show = _adapter.GetShow(id);

            return View(show);
        }
        [HttpPost]
        //public ActionResult EditShow(int id, string title, string synopsis, string imageURL)
        public ActionResult EditShow(TVShow show)
        {
            show = _adapter.UpdateShow(show);
            
            return RedirectToAction("Detail", new { id = show.Id });
        }

        //TV Show Delete action
        [HttpPost]
        public ActionResult DeleteShow(int id)
        {
            _adapter.DeleteShow(id);

            return RedirectToAction("Index");
        }

    }
}